<?php
include_once IR.'functions/common/cache_info.func.php';
include_once IR.'functions/user/user_info.func.php';

//设置用户session
//设置用户cache
function init_user_session_and_cache($uid,$sid)
{
    global $zeit;
    set_cache($uid,'online_info',$sid,1);
    $user_base = user_get_user_base($uid,1);
}
//获取玩家基本信息
function check_user_online($uid,$sid)
{
    //先检查memcache是否存在
    $temp_uid = check_user_session($sid,$uid);
    if($temp_uid==0 || $uid != $temp_uid)
    {
        return 0;
    }else{
        $user_base = user_get_user_base($uid,1);
        return $user_base;
    }
}

function check_user_session($sid,$uid)
{
    //temp code	 
    $temp_sid = get_cache($uid,'online_info',1);
    if ($temp_sid  ==-1){
	return 0;
    } elseif ($temp_sid  ==-2) {
        //没有缓存信息或超时，我需要重新刷一次自己的信息
        set_cache($uid,'online_info',$sid,1);
	$this_uid = $uid;
    } else {
        if ($sid==$temp_sid['data']){
            $this_uid  = $uid;
        } else {
            $this_uid = 0;
        }
        
    }
    return $this_uid;
}

function gen_session_id($uid,$ts,$key)
{
    global $zone_id;
    $sid = substr(md5($zone_id.$uid.$key.$ts),2,12);
    return $sid;
}

function gen_online_id($uid,$sid,$zeit,$uuid){
    $ipx = get_user_ip();
    $sql = "INSERT INTO st_online_details
			(uid,sid,first_online,last_online,ip1,ip2,ip3,ip4,ip_all,uuid)
			VALUES
			($uid,'$sid',$zeit,$zeit,'$ipx[0]','$ipx[1]','$ipx[2]','$ipx[3]','$ipx[4]','$uuid')";
    $rst = mysql_w_query($sql);
    return mysql_insert_id();
}

function update_online_info($uid,$user_extend,$zeit,$online_id){
    global $session_config;
    if ($zeit - $user_extend['last_online'] > $session_config['memcache_sync_time']){
        $change['last_online'] = $zeit;
        user_write_back_extend_data($uid,$user_extend, $change);
        $sql = "UPDATE st_online_details SET last_online = $zeit WHERE 	id=$online_id";
        mysql_x_query($sql);
    }
}
?>